package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import persistence.exception.DAOException;
import persistence.util.JdbcUtil;

public class PeriodicalDAOJdbcImpl extends JdbcDAOBase implements PeriodicalDAO{
	
	private PredefinedEntryDAO predefinedEntryDAO;	
	
	public PredefinedEntryDAO getPredefinedEntryDAO() {
		return predefinedEntryDAO;
	}

	public void setPredefinedEntryDAO(PredefinedEntryDAO predefinedEntryDAO) {
		this.predefinedEntryDAO = predefinedEntryDAO;
	}

	public PeriodicalDAOJdbcImpl() {
		super();
	}

	@Override
	public Periodical find(int id) throws DAOException{
		String sql = "SELECT id, titel, verlag, issn, erscheinungsintervall, ac_nummer," +
					 " erscheinungsform, online_zugang, zugangsdaten, eissn, webadresse," +
					 " in_ezb, han, anmerkungen FROM zeitschriften WHERE id = ? ORDER BY titel ASC";
		Connection c = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		Periodical periodical = null;
		PredefinedEntry publicationInterval = null;
		PredefinedEntry kindOfPublication = null;
		PredefinedEntry kindOfOnlineAccess = null;
		
		try {
			c = getConProvider().getConnection();
			prepStmt = c.prepareStatement(sql);
			prepStmt.setInt(1, id);
			rs = prepStmt.executeQuery();
			
			if (rs.next()) {
				publicationInterval = predefinedEntryDAO.find(rs.getInt("erscheinungsintervall"));
				kindOfPublication = predefinedEntryDAO.find(rs.getInt("erscheinungsform"));
				kindOfOnlineAccess = predefinedEntryDAO.find(rs.getInt("online_zugang"));
				periodical = new Periodical (rs.getInt("id"), rs.getString("titel"), rs.getString("verlag"), rs.getString("issn"), publicationInterval, rs.getString("ac_nummer"),
											 kindOfPublication, kindOfOnlineAccess, rs.getString("zugangsdaten"), rs.getString("eissn"), rs.getString("webadresse"), 
											 rs.getBoolean("in_ezb"), rs.getBoolean("han"), rs.getString("anmerkungen"));
			}
		}
		catch (SQLException se) {
			throw new DAOException("Problem when attempting to find Periodical with id: " + id, se);
		}
		finally {
			JdbcUtil.cleanup(c, prepStmt, rs);
		}
		return periodical;
	}

	@Override
	public List<Periodical> findAllByTitle(String title) throws DAOException{
		
		System.out.println("------------- CALLING findAllByTitle() ------------");
		
		String sql = "SELECT id, titel, verlag, issn, erscheinungsintervall, ac_nummer," +
		 			 " erscheinungsform, online_zugang, zugangsdaten, eissn, webadresse," +
		 			 " in_ezb, han, anmerkungen FROM zeitschriften WHERE titel LIKE ? ORDER BY titel ASC";
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		List<Periodical> periodicals = new ArrayList<Periodical>();
		PredefinedEntry publicationInterval = null;
		PredefinedEntry kindOfPublication = null;
		PredefinedEntry kindOfOnlineAccess = null;
		
		try {
			con = getConProvider().getConnection();
			prepStmt = con.prepareStatement(sql);
			prepStmt.setString(1, title+"%");
			rs = prepStmt.executeQuery();
			
			while (rs.next()) {
				publicationInterval = predefinedEntryDAO.find(rs.getInt("erscheinungsintervall"));
				kindOfPublication = predefinedEntryDAO.find(rs.getInt("erscheinungsform"));
				kindOfOnlineAccess = predefinedEntryDAO.find(rs.getInt("online_zugang"));
				
				
				periodicals.add(new Periodical (rs.getInt("id"), rs.getString("titel"), rs.getString("verlag"), rs.getString("issn"), publicationInterval,
												rs.getString("ac_nummer"), kindOfPublication, kindOfOnlineAccess, rs.getString("zugangsdaten"), rs.getString("eissn"),
												rs.getString("webadresse"), rs.getBoolean("in_ezb"), rs.getBoolean("han"), rs.getString("anmerkungen")));
//				//////////////////////REFERENCE Test
//				intermediatePeriodical = null;
			}			
		}
		catch (SQLException se) {
			se.printStackTrace();
			throw new DAOException("Error when attempting to find all Periodicals with title: " + title, se);
		}
		finally {
			JdbcUtil.cleanup(con, prepStmt, rs);
		}
		return periodicals;
	}

	@Override
	public List<Periodical> findAll() throws DAOException {		
		return findAllByTitle("");
	}

	@Override
	public void update(Periodical periodical) throws DAOException{
		// TODO Auto-generated method stub
		
	}

	@Override
	public Periodical create() throws DAOException{
		// TODO Auto-generated method stub
		return null;
	}

}
